#include<cstdio>
using namespace std;
const int maxn=1e4+1;
bool visit[maxn]={false},ans[maxn]={false};
int father[maxn];
int N,Q;

int findFather(int x){
    int a=x;
    while(x!=father[x]){
        x=father[x];
    }
    while(a!=father[a]){
        int z=a;
        father[z]=x;
        a=father[a];
    }
    return x;
}
void Union(int x,int y){
    int fx=findFather(x);
    int fy=findFather(y);
    if(fx!=fy){
        father[fx]=fy;
    }
}

int main(){
    //freopen("in.txt","r",stdin);
    for(int i=1;i<=maxn;i++){
        father[i]=i;
    }
    scanf("%d",&N);
    for(int i=0;i<N;i++){
        int u,v;
        scanf("%d %d",&u,&v);
        visit[v]=true;
        for(int j=0;j<u-1;j++){
            int t;
            scanf("%d",&t);
            visit[t]=true;
            Union(v,t);
        }
    }
    int cnt=0;
    for(int i=1;i<maxn;i++){
        if(visit[i]){
            cnt++;
            ans[findFather(i)]=true;
        }
    }
    int numtree=0;
    for(int i=1;i<maxn;i++){
        if(ans[i]) numtree++;
    }
    printf("%d %d\n",numtree,cnt);
    scanf("%d",&Q);
    for(int i=0;i<Q;i++){
        int first,second;
        scanf("%d %d",&first,&second);
        if(findFather(first)!=findFather(second)){printf("No\n");}
        else {printf("Yes\n");}
    }
}
